/**
  ******************************************************************************
  * File Name          : weight.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "route_expio_485.h"

TaskHandle_t xRoute_485_Thd = NULL;
StaticTask_t xRoute_485_Tcb;
StackType_t  xRoute_485_Stk[ROUTE_485_STACK_SIZE];

/* 485链路接收 */
QueueHandle_t Route_485RxLink_Qhd = NULL;
static StaticQueue_t route_485_rx_link_stru;
static uint8_t       route_485_rx_link_area[ROUTE_485RXLINK_QUE_LENGTH * ROUTE_485RXLINK_QUE_ITEMSIZE];

/* 485 接收消息队列 */
Route_PayloadType Route_485_rx_link_isr;
Route_PayloadType Route_485_rx_link_bck;
Route_PayloadType Route_send_to_485;

void vRoute_485_Task(void * pvParameters)
{
  uint8_t err;
  
	RS485_GetLocalAddress();
	
  Route_485RxLink_Qhd = xQueueCreateStatic(ROUTE_485RXLINK_QUE_LENGTH, ROUTE_485RXLINK_QUE_ITEMSIZE,
	                                         route_485_rx_link_area, &route_485_rx_link_stru);
  RS485_BspInit();
  
  while(1)
	{
		if(xQueueReceive(Route_485RxLink_Qhd, &Route_485_rx_link_bck, portMAX_DELAY) == pdTRUE)
    {
      if(Route_PayloadCRC(&Route_485_rx_link_bck, ROUTE_PLCRC_CHECK) == 0)
      {
        LED2(LED_On);
        
        switch(Route_485_rx_link_bck.pl_type)
        {
          case ROUTE_PLTYP_UN_PING:
            Route_send_to_485.trans_dir = ROUTE_TRSDIR_SUBDEV_TO_LOCAL;
            Route_send_to_485.pl_type   = ROUTE_PLTYP_UN_RESPING;
            Route_send_to_485.pl_len    = 0;
            RS485_LinkSend(&Route_send_to_485);
            break;
          
          case ROUTE_PLTYP_UN_BCTIME:
            Sys_Adjtime.hour   = Route_485_rx_link_bck.payload[0];
            Sys_Adjtime.minute = Route_485_rx_link_bck.payload[1];
            Sys_Adjtime.second = Route_485_rx_link_bck.payload[2];
            Sys_SetDataTime();
            break;
          
          default:
            err = Route_expio_xx_rxprocess(&Route_485_rx_link_bck, &Route_send_to_485);
            if(err != 0)
            {
              Route_send_to_485.trans_dir = ROUTE_TRSDIR_SUBDEV_TO_LOCAL;
              Route_send_to_485.pl_type   = ROUTE_PLTYP_UN_ERROR;
              Route_send_to_485.pl_len    = sizeof(Route_pl_un_msg_Type);
              ((Route_pl_un_msg_Type *)(Route_send_to_485.payload))->msg = err;
            }
            RS485_LinkSend(&Route_send_to_485);
            break;
        }
        
        LED2(LED_Off);
      }
    }
  }
}

void Route_SubDevInstructLed(void)
{
  uint8_t i;
  
  for(i=0; i<Route_LocalDevType; i++)
  {
    LED1(LED_On);
    LED2(LED_On);
    vTaskDelay(200);
    
    LED1(LED_Off);
    LED2(LED_Off);
    vTaskDelay(200);
  }
}

